<template>
  <el-dialog custom-class="adup-money-type" v-el-drag-dialog="{ drag: true, full: false, resize: false }" :title="title"
    :visible="true" :close-on-click-modal="false" @close="closeMd" :width="'400px'">
    <el-form :model="fm.ps" :ref="fm.fname" @keyup.enter.native="onSubmit" :label-width="'100px'">
      <el-form-item v-if="myData.pobj" label="父级类型" prop="typeName">
        【{{ myData.pobj.typeName }}】类型
      </el-form-item>

      <el-form-item label="金额类型" prop="typeName" :rules="[{ required: true, message: '金额类型名称不能为空', trigger: 'blur' }]">
        <el-input v-model="fm.ps.typeName" placeholder="请输入金额类型名称">
        </el-input>
      </el-form-item>

      <el-form-item v-if="myData.type == 0 || myData.type == 2" label="利润方向" prop="profitDirection"
        :rules="[{ required: true, message: '利润方向不能为空', trigger: 'blur' }]">
        <el-select :disabled="(myData.type == 2 && myData.obj.upTypeId) ? true : false" v-model="fm.ps.profitDirection"
          style="width: 100%" clearable placeholder="请选择利润方向">
          <el-option v-for="it in profintOptions" :key="it.value" :label="it.label" :value="it.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" :loading="fm.sb.load" @click="onSubmit">
          {{ fm.sb.text }}
        </el-button>
        <el-button @click="closeMd">关闭</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import elDragDialog from "@/directive/el-drag-dialog";

import * as systemSer from "@/api/services/system.service";

export default {
  name: "AdupMoneyType",
  directives: { elDragDialog },
  props: { myData: Object },
  data() {
    return {
      title: this.myData.obj ? '修改-金额类型' : '添加-金额类型',

      fm: {
        fname: "adupMoneyTypeFm",
        sb: { load: false, text: this.myData.obj ? '修改' : '添加' },
        ps: {
          // 修改id
          updId: '',
          // 父id
          pid: '',
          // 利润方向
          profitDirection: '',
          // 类型名称
          typeName: "",
        },
      },

      // 利润方向-选择数据
      profintOptions: [
        { value: 'INCOME', label: '收入类' },
        { value: 'OUTPAY', label: '支出类' },
        { value: 'TRANS', label: '互转类' },
      ],

    };
  },
  created() {
    this.init();
  },
  methods: {
    /* 关闭弹框 */
    closeMd() {
      this.$emit("myClick", { state: false });
    },
    // 初始化数据
    init() {
      const that = this, ps = that.fm.ps, { type } = that.myData;

      if (type == 0) {// 添加一级金额类型
        ps.pid = 0;
      } else if (type == 1) {// 为指定金额类型添加子级
        let po = that.myData.pobj;
        // 父id
        ps.pid = po.id;
      } else if (type == 2) {// 修改指定金额类型
        let o = that.myData.obj;
        // 修改id
        ps.updId = o.id;
        // 父id
        ps.pid = o.upTypeId?.id || 0;
        // 利润方向
        ps.profitDirection = o.profitDirection;
        // 类型名称
        ps.typeName = o.typeName;
      }
    },
    // 提交表单
    onSubmit() {
      let that = this, U = that.U, fm = that.fm, sb = fm.sb, ps = U.scopy(fm.ps);

      that.$refs[fm.fname].validate((valid) => {
        if (!valid) { return false; } else {
          sb.load = true;

          if (!ps.updId) {// 添加
            let p = { pid: ps.pid, typeName: ps.typeName };
            if (ps.pid == 0) p.profitDirection = ps.profitDirection;
            systemSer.addMoneyType(p).then(res => {
              sb.load = false;

              if (res.code != 1) {
                U.qerror(res.msg);
              } else {
                U.qsuccess(res.msg);

                // 关闭弹框
                that.closeMd();

                // 调用父组件方法-重新加载数据
                that.$emit("reloadDg");
              }
            });
          } else {// 修改
            let p = { updId: ps.updId, pid: ps.pid, typeName: ps.typeName };
            if (ps.pid == 0) p.profitDirection = ps.profitDirection;
            systemSer.updateMoneyType(p).then(res => {
              sb.load = false;

              if (res.code != 1) {
                U.qerror(res.msg);
              } else {
                U.qsuccess(res.msg);

                // 关闭弹框
                that.closeMd();

                // 调用父组件方法-重新加载数据
                that.$emit("reloadDg");
              }
            });
          }

          setTimeout(() => { sb.load = false; }, 10000);
        }
      });
    },


  },
};
</script>
